package com.borya.dao.impl;

import com.alibaba.fastjson.JSONObject;
import com.borya.constant.RedisConstant;
import com.borya.dao.MessageDAO;
import com.borya.framework.annotation.Component;
import com.borya.framework.core.ServerSqlSession;
import com.borya.model.db.Message;
import com.borya.model.db.WelcomeMsg;

import java.util.*;
import java.util.stream.Collectors;

@Component("messageDAO")
public class MessageDAOImpl extends BaseDAOImpl implements MessageDAO{
	
	@Override
	public boolean save(Message msg) {
		StringBuilder sql = new StringBuilder(256);
		sql.append("insert tb_message(");
		sql.append("msg_id,msg_type,sender,receiver,create_time,exp_time,content");
		sql.append(")values(");
		sql.append("'"+msg.getMsgId()+"'");
		sql.append(",'"+msg.getMsgType()+"'");
		sql.append(",'"+msg.getSender()+"'");
		sql.append(",'"+msg.getReceiver()+"'");
		sql.append(",'"+msg.getCreatTime()+"'");
		sql.append(",'"+msg.getExpTime()+"'");
		sql.append(",'"+msg.getContent()+"'");
		sql.append(")");
	
		return executeUpdate(sql.toString());
	}


	@Override
	public List<WelcomeMsg> allWelComeMsg() {
		final String key = RedisConstant.WELCOME_MSG;
		Map<String,String> cache = redis_hgetAll(key);
		if(Objects.nonNull(cache) && !cache.isEmpty()){
			redis_expire(key,3600);
			return cache.entrySet().parallelStream()
					.map(x->JSONObject.parseObject(x.getValue(),WelcomeMsg.class))
					.collect(Collectors.toList());
		}
		try (ServerSqlSession session = getSqlDataSource().session();){
			StringBuilder sb = new StringBuilder(128);
			sb.append("select * from tb_message_welcome");
			Map<String,WelcomeMsg> msgMap = session.executeQuery(sb.toString(),rs->{
				if(rs != null){
					Map<String,WelcomeMsg> map = new HashMap<>(8);
					while(rs.next()){
						WelcomeMsg msg = new WelcomeMsg();
						msg.setId(rs.getInt("id"));
						msg.setMsgId(rs.getString("msg_id"));
						msg.setContent(rs.getString("content"));
						msg.setMsgType(rs.getInt("msg_type"));
						msg.setSender(rs.getString("sender"));
						msg.setIsMain(rs.getInt("is_main"));
						msg.setDetailType(rs.getInt("detail_type"));
						msg.setFlag(rs.getInt("flag"));
						msg.setValueTime(rs.getInt("value_time"));
						map.put(rs.getString("id"),msg);
					}
					return map;
				}
				return null;
			});
			if(!msgMap.isEmpty()){
				cache = msgMap.entrySet().parallelStream()
						.collect(Collectors.toMap(x->x.getKey(),x->JSONObject.toJSONString(x.getValue())));
				redis_hmset(key,cache);
				redis_expire(key,3600);
			}
			return msgMap.entrySet().parallelStream().map(x->x.getValue()).collect(Collectors.toList());
		} catch (Exception e){
			return null;
		}
	}

	@Override
	public boolean addWelcomeMsg(Message message, List<WelcomeMsg> welcomeMsgList) {
		StringBuilder sql = new StringBuilder(128);
		sql.append("insert tb_message(");
		sql.append("msg_id,msg_type,sender,receiver,create_time,exp_time,content");
		sql.append(")values ");
		sql.append(welcomeMsgList.stream()
				.map(msg->
					String.format("('%s',%s,'%s','%s',%s,%s,'%s')",msg.getMsgId(),
							msg.getMsgType(),msg.getSender(),message.getReceiver(),
							message.getCreatTime(),message.getCreatTime()+msg.getValueTime()*1000,msg.getContent())
				).collect(Collectors.joining(",")));

		return executeUpdate(sql.toString());
	}
}